package com.example.lianjuan.Presenter.impl.impl;

import com.example.lianjuan.Model.API;
import com.example.lianjuan.Model.domain.SelectCategories;
import com.example.lianjuan.Model.domain.SelectContent;
import com.example.lianjuan.Presenter.impl.ISelectPresenter;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.RetrofitManang;
import com.example.lianjuan.Utils.UtilsUrl;
import com.example.lianjuan.View.ISelectCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SelectPresenterImpl implements ISelectPresenter {

    private ISelectCallback mCallback;
    private final API mAPI;
    private int mItemId;

    public SelectPresenterImpl() {
        Retrofit retrofit = RetrofitManang.getRetrofitManang().getRetrofit();
        mAPI = retrofit.create(API.class);
    }

    @Override
    public void getSelectCategories() {
        if (mCallback != null) {
            mCallback.onLoadLoading();
        }
        Call<SelectCategories> task = mAPI.getSelectCategories();
        task.enqueue(new Callback<SelectCategories>() {
            @Override
            public void onResponse(Call<SelectCategories> call, Response<SelectCategories> response) {
                int code = response.code();
                LogUtils.d(SelectPresenterImpl.this, "code----->" + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    //请求成功
                    SelectCategories body = response.body();
                    LogUtils.d(SelectPresenterImpl.this, "body----->" + body.toString());
                    //传给UI层精选分类
                    if (mCallback != null) {
                        mCallback.getSelectCategories(body);
                    }
                } else {
                    //请求失败
                    handlerLoadError();
                }
            }

            @Override
            public void onFailure(Call<SelectCategories> call, Throwable t) {
                //请求失败
                handlerLoadError();
            }
        });
    }

    @Override
    public void getContentByCategoriesID(SelectCategories.DataDTO item) {
        mItemId = item.getFavorites_id();
        String url = UtilsUrl.getSelectPager(item.getFavorites_id());
        LogUtils.d(this, "url--->" + url);
        Call<SelectContent> task = mAPI.getSelectContent(url);
        task.enqueue(new Callback<SelectContent>() {
            @Override
            public void onResponse(Call<SelectContent> call, Response<SelectContent> response) {
                int code = response.code();
                LogUtils.d(SelectPresenterImpl.this, "code----->" + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    //请求成功
                    SelectContent body = response.body();
                    LogUtils.d(SelectPresenterImpl.this, "body----->" + body.toString());
                    //传给UI层精选分类
                    if (mCallback != null) {
                        mCallback.getSelectContent(body);
                    }
                } else {
                    //请求失败
                    handlerLoadError();
                }
            }

            @Override
            public void onFailure(Call<SelectContent> call, Throwable t) {
                handlerLoadError();
            }
        });
    }

    private void handlerLoadError() {
        if (mCallback != null) {
            mCallback.onLoadError();
        }
    }

    @Override
    public void reloadContent() {
        if (mCallback != null) {
            getSelectCategories();
        }
    }

    @Override
    public void registerViewCallback(ISelectCallback callback) {
        mCallback = callback;
    }

    @Override
    public void unregisterViewCallback(ISelectCallback callback) {
        mCallback = null;
    }
}
